package org.eclipse.wtp.sample.converter;

import java.io.IOException;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ConvertServlet extends HttpServlet implements Servlet {

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		//super.doPost(request, response);
		//TODO add String Type
		//double converTo = Double.parseDouble(request.getParameter("convertTo"));
		String type= request.getParameter("type");
		//try{
		double convertFrom= Double.parseDouble(request.getParameter("convertFrom"));
		//	System.out.println(convertFrom);
		//}
		//catch(java.lang.NumberFormatException nFE) {
		//	System.out.println("Not a Number");
		//}
		int unitsFrom = Integer.parseInt(request.getParameter("unitsFrom"));
		int unitsTo	  = Integer.parseInt(request.getParameter("unitsTo"));
		String message= "Sorry Wrong Input";
		// check the variable first

				
		ConverterClass convert= (ConverterClass)request.getSession(true).getAttribute("convert");
		if(convert == null)
		{
			convert= new ConverterClass(null, convertFrom,unitsTo,unitsFrom);
		}
		else
		{
			convert.setConvertFrom(convertFrom);
			convert.setUnitsFrom(unitsFrom);
			convert.setUnitsTo(unitsTo);
		}
		// TODO implement this section...
		// Call the convert on converFrom with the units and stuff.
		// Also set the Units from strings to ints...if needed.
		//convert.setConvertTo(unitsFrom);// THis needs to change... Just want to test first
		convert.setUnit(unitsTo, unitsFrom);
		switch (unitsFrom)
		{
		case 1:
		case 2:
			convert.ConvertDistance(convertFrom, unitsTo, unitsFrom);
			break;
		case 3:
		case 4:
		case 5:
			convert.tempconv(convertFrom, unitsFrom, unitsTo);
			break;
		case 6:
		case 7:
			convert.ConvertMass(convertFrom, unitsFrom, unitsTo);
			break;
		case 10:
		case 11:
			convert.ConvertVelocity(convertFrom, unitsFrom, unitsTo);
			break;
		case 12:
		case 13:
			convert.ConvertAngle(convertFrom, unitsFrom, unitsTo);
			break;
		case 14:
		case 15:
		case 16:
			convert.ConvertAngle(convertFrom, unitsFrom, unitsTo);
			
		
		}
		
		request.getSession().setAttribute("convert", convert);
		getServletContext().getRequestDispatcher("/Convert.jsp").forward(request,response);
		
		
		
	}
	

}
